package com.example.lin.circleprogressbar;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.lin.circleprogressbar.view.CircleProgressBar;

public class MainActivity extends AppCompatActivity {

    private CircleProgressBar cpb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cpb = findViewById(R.id.cpb);


        startDownload();
    }

    private void startDownload() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= cpb.getMax(); i++) {
                    Message message = h.obtainMessage(123);
                    message.obj = i;
                    h.sendMessage(message);
                    SystemClock.sleep(50);
                }
            }
        }).start();
    }


    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 123:
                    int progress = (int) msg.obj;
                    cpb.setProgress(progress);
                    break;
            }
            return false;
        }
    });

}
